<template>
  <div v-bind="$attrs">
    <el-button :type="type" :loading="loading" @click="handleExport">导出</el-button>
    <a ref="export-a" :href="path" class="export-a" target="_blank" rel="noopener noreferrer" style="display:none;" />
  </div>
</template>

<script>
import request from "@rabbit/utils/request";

export default {
  name: "Export",

  props: {
    url: {
      type: String,
      required: true,
    },
    params: {
      type: Object,
      default: () => ({}),
    },
    type: {
      type: String,
      default: "",
    },
    host: {
      type: String,
      required: true,
    },
  },

  data() {
    return {
      path: "",
      loading: false,
    };
  },

  methods: {
    handleExport() {
      this.loading = true;
      request({ url: this.url, data: { ...this.params }}).then(res => {
        this.loading = false;
        if (res.statusCode === 600) {
          this.path = this.host + res.data;
          this.$refs["export-a"].click();
        }
      });
    },
  },
};
</script>
